﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using IBLL;
using MisDLL;
using System.Data;

namespace MisBLL
{
    public class AreaDAO : MarshalByRefObject, IArea
    {
        protected DbHelper dbHelper = new DbHelper();

        //根据编码查询库位信息
        public MisModel.Area GetById(string id)
        {
            if (string.IsNullOrEmpty(id))
                return null;
            string sql = string.Format("select * from AREA_TAB where AREA_id='{0}'", id);
            DataTable dt=  dbHelper.ExecuteDataTable(sql);
            MisModel.Area entity = null;
            if (dt != null && dt.Rows.Count > 0){
                entity = new MisModel.Area();
                entity.AreaID = dt.Rows[0]["AREA_id"].ToString();
                entity.AreaName = dt.Rows[0]["AREA"].ToString();
                entity.F1 = dt.Rows[0]["F1"].ToString();
            }
            return entity;
        }

        //查询库位列表
        public DataTable GetAll()
        {
            string sql = "select AREA_id,AREA,F1,(AREA_id + '.' + AREA) as ItemShow  from AREA_TAB";
            return dbHelper.ExecuteDataTable(sql);
        }

        //添加库位信息
        public int Add(MisModel.Area obj)
        {
            if (string.IsNullOrEmpty(obj.F1.ToString()))
            {
                obj.F1 = "Y";
            }
            string sql = string.Format("insert into AREA_TAB(AREA_id,AREA,F1) values('{0}','{1}','{2}')", obj.AreaID, obj.AreaName, obj.F1);
            return dbHelper.ExecuteNonQuery(sql);
        }

        //修改库位信息
        public int Update(MisModel.Area obj)
        {
            string sql = string.Format("update AREA_TAB set AREA='{0}',F1='{1}' where AREA_id='{2}'", obj.AreaName, obj.F1, obj.AreaID);
            return dbHelper.ExecuteNonQuery(sql);
        }

        //根据ID删除库位信息
        public int Delete(string id)
        {
            string sql = string.Format("delete from AREA_TAB where AREA_id='{0}'", id);
            return dbHelper.ExecuteNonQuery(sql);
        }
    }
}
